#include "levelactor.h"
#include "level.h"
#include "levelobjectselectionmanager.h"
#include "gameObjects/visualobject.h"
#include "gameObjects/actor.h"

using namespace LevelObjects;

LevelActor::LevelActor(QObject *parent, QPointer<AbstractGameObject> actor, const QString& name)
	: AbstractLevelObject(parent, name)
	, m_actor(actor)
	, m_name(name)
	, m_fAngle(0.0f)
	, m_nZDepth(0)
{

}

LevelObjects::LevelObjectType LevelObjects::LevelActor::type() const
{
	return LOT_Actor;
}

QPointer<Level> LevelObjects::LevelActor::level() const
{
	return qobject_cast<Level*>(parent());
}

QRect LevelObjects::LevelActor::actorVisualData( QImage& image )
{
	QRect rect;
	QPointer<Actor> actorGameObject = qobject_cast<Actor*>(m_actor);
	const Actor::VisualObjects& voList = actorGameObject->visualObjects();

	if(voList.size())
	{
		QPointer<VisualObject> vo = qobject_cast<VisualObject*>(voList.first());

		if(vo)
		{
			image = vo->imageData().image();
			if(!vo->isAnimation())
			{
				rect = image.rect();
			}
			else
			{
				const VisualObject::Frames& frames = vo->frames();
				Q_ASSERT(frames.size() > 0);
				rect = frames.first();
			}
		}
	}

	return rect;
}

bool LevelObjects::LevelActor::isSelected() const
{
	return level()->selectionManager()->isSelected(qobject_cast<AbstractLevelObject*>(const_cast<LevelActor*>(this)));
}
